package com.teragadgets.android.comic;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import com.teragadgets.android.comic.datamodel.WebComic;
import com.teragadgets.android.comic.datamodel.WebComicDatabase;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class WebComicManagerActivity extends Activity implements Observer,
		OnClickListener {

	ListView webComicList;
	WebComicManagerModel model;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manager);

		// Initialize Model
		model = new WebComicManagerModel(this);
		model.addObserver(this);

		webComicList = (ListView) this.findViewById(R.id.comicList);
		webComicList.setAdapter(new ComicAdapter(this, model.getWebComics(),
				R.layout.managerrow, R.id.webComicName));

		Button cancelButton = (Button) this.findViewById(R.id.cancel);
		cancelButton.setOnClickListener(this);
		Button saveButton = (Button) this.findViewById(R.id.save);
		saveButton.setOnClickListener(this);
	}

	@Override
	public void update(Observable observable, Object data) {
		((ComicAdapter) webComicList.getAdapter()).notifyDataSetChanged();
		webComicList.refreshDrawableState();
	}

	private WebComicManagerActivity getThis() {
		return this;
	}

	private class ComicAdapter extends ArrayAdapter<WebComic> implements
			OnCheckedChangeListener, OnClickListener {

		public ComicAdapter(Context context, Vector<WebComic> webComics,
				int layout, int textResource) {
			super(context, layout, textResource, webComics);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = super.getView(position, convertView, parent);

			ImageView logo = (ImageView) view.findViewById(R.id.webComicLogo);
			Bitmap icon = getItem(position).getIcon();
			if (icon == null)
				getItem(position).addObserver(getThis());
			logo.setImageBitmap(icon);

			CheckBox checkbox = (CheckBox) view.findViewById(R.id.comic_check);
			checkbox.setChecked(getItem(position).getSelected());
			checkbox.setTag(position);
			checkbox.setOnClickListener(this);

			return view;
		}

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			getItem((Integer) (buttonView.getTag())).setSelected(isChecked);

		}

		@Override
		public void onClick(View v) {
			CheckBox checkbox = (CheckBox) v;
			getItem((Integer) (checkbox.getTag())).setSelected(
					checkbox.isChecked());
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancel:
			this.finish();
			return;
		case R.id.save:
			WebComicDatabase db = WebComicDatabase.open(this);
			Vector<WebComic> webComics = model.getWebComics();
			for (WebComic c : webComics) {
				if (c.getSelected())
					db.updateFromAppEngine(c);
				else
					db.deleteComic(c.getID());
			}
			db.close();
			this.finish();
			return;
		}
	}
}
